package top.zhangjianyong.tools.wechat;

import com.alibaba.dashscope.common.Message;
import top.zhangjianyong.tools.generation.GenerationMessage;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author zhangjianyong
 * @since 2025/3/21 上午11:59
 */
public class WechatContentHolder {
    private final static Map<String, WechatContent> WECHAT_CONTENT_MAP = new HashMap<>();


    public static WechatContent get(String openId) {
        if (WECHAT_CONTENT_MAP.containsKey(openId)) {
            WechatContent wechatContent = WECHAT_CONTENT_MAP.get(openId);
            if (wechatContent.isExpire()) {
                return createNew(openId);
            }
            return wechatContent;
        } else {
            return createNew(openId);
        }
    }

    public static void remove(String openId) {
        WECHAT_CONTENT_MAP.remove(openId);
    }

    private static WechatContent createNew(String openId) {
        List<Message> messages = GenerationMessage.createMessages();
        WechatContent wechatContent = new WechatContent(openId, messages);
        WECHAT_CONTENT_MAP.put(openId, wechatContent);
        return wechatContent;
    }

}
